package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import com.datamation.swadeshi.model.Items;
import com.datamation.swadeshi.model.LoadingCustom;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class LoadingCustomDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	
	public LoadingCustomDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException 
	{
		dB = dbHelper.getWritableDatabase();
	}
	@SuppressWarnings("static-access")
	public ArrayList<LoadingCustom> getAllItemLoading(String itmename,String RefNo) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<LoadingCustom> list = new ArrayList<LoadingCustom>();
		
		String selectQuery = "select a.ItemName,a.ItemCode,a.NOUCase,b.QOH,b.LocCode from fItem as a inner join fItemLoc as b on a.ItemCode = b.ItemCode where b.LocCode = 'MS' AND a.ItemCode not in (SELECT DISTINCT itemcode FROM FtranDet WHERE RefNo='"+RefNo+"') AND(a.ItemName LIKE '%"+itmename+"%' OR a.ItemCode LIKE '%"+itmename+"%')";
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			LoadingCustom items=new LoadingCustom();
		
			items.setSTOCK_LOADING_ITEMNAME(cursor.getString(cursor.getColumnIndex(dbHelper.FITEM_ITEM_NAME)));
			items.setSTOCK_LOADING_ITEMCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FITEM_ITEM_CODE)));
			items.setSTOCK_LOADING_ITEMNOUCASE(cursor.getString(cursor.getColumnIndex(dbHelper.FITEM_NOU_CASE)));
			items.setSTOCK_LOADING_QOH(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_QOH)));
			items.setSTOCK_LOADING_ITEMLOCCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_LOC_CODE)));

					
			list.add(items);
			
		}
		
		return list;
	}
	@SuppressWarnings("static-access")
	public String getItemNameByCode(String code)
	{
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FITEM +" WHERE "+dbHelper.FITEM_ITEM_CODE+"='"+code+"'" ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FITEM_ITEM_NAME));
			
		}
			
		return "";
	}
	@SuppressWarnings("static-access")
	public String getItemUnitsByCode(String code)
	{
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "select * from fItem as a inner join fItemLoc as b on a.ItemCode = b.ItemCode where b.LocCode = 'MS' and a.itemCode = '"+code+"'" ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FITEM_NOU_CASE));
			
		}
			
		return "";
	}
	
	
	@SuppressWarnings("static-access")
	public String getItemQohByCode(String code)
	{
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "select * from fItem as a inner join fItemLoc as b on a.ItemCode = b.ItemCode where b.LocCode = 'MS' and a.itemCode = '"+code+"'" ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_QOH));
			
		}
			
		return "";
	}
	
	@SuppressWarnings("static-access")
	public String getItemQohByCodeUnLoad(String itemcode,String LocCode)
	{
		
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		String selectQuery = "select * from fItem as a inner join fItemLoc as b on a.ItemCode = b.ItemCode where b.LocCode = '"+LocCode+"' and a.itemCode = '"+itemcode+"'" ;
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){

			return cursor.getString(cursor.getColumnIndex(dbHelper.FITEMLOC_QOH));
			
		}
			
		return "";
	}
	
	
	

}
